{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "实时查询10年和3月利差的值网址：https://iftp.chinamoney.com.cn/chinese/bkcurvrty/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0.25: 1.4221, 10.0: 1.75}\n",
      "tl=10 的 buyYieldStr 减去 tl=0.25 的 buyYieldStr 的结果为: 0.327900\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "# https://iftp.chinamoney.com.cn/chinese/bkcurvrty/\n",
    "\n",
    "# 定义请求的URL\n",
    "url = \"https://iftp.chinamoney.com.cn/ags/ms/cm-u-bk-currency/RtimeYldCurv\"\n",
    "\n",
    "# 设置请求参数\n",
    "params = {\n",
    "    \"lang\": \"CN\",\n",
    "    # \"qttndate\": \"2025-01-10\",  # 查询日期\n",
    "    # \"qttntime\": \"13:30\",       # 查询时间\n",
    "    # \"bondType\": \"100101\"       # 国债类型\n",
    "}\n",
    "\n",
    "# 发送GET请求\n",
    "response = requests.get(url, params=params)\n",
    "\n",
    "# 检查请求是否成功\n",
    "if response.status_code == 200:\n",
    "    data = response.json()  # 解析JSON数据\n",
    "    records = data.get(\"records\", [])  # 获取records字段\n",
    "\n",
    "    # 筛选出 tl=0.25 和 tl=10 的对象\n",
    "    buy_yields = {record.get(\"tl\"): float(record.get(\"buyYieldStr\")) for record in records if record.get(\"tl\") in [0.25, 10]}\n",
    "    print(buy_yields)\n",
    "\n",
    "    # 确保两个对象都存在\n",
    "    if 0.25 in buy_yields and 10 in buy_yields:\n",
    "        result = buy_yields[10] - buy_yields[0.25]\n",
    "        print(f\"tl=10 的 buyYieldStr 减去 tl=0.25 的 buyYieldStr 的结果为: {result:.6f}\")\n",
    "    else:\n",
    "        print(\"未找到完整的 tl=0.25 和 tl=10 对象数据。\")\n",
    "else:\n",
    "    print(f\"请求失败，状态码: {response.status_code}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
